<%@ page language="java" contentType="text/html; charset=EUC-KR"
    pageEncoding="EUC-KR"%>
<%@ page import="java.net.URLEncoder"%>
<%@ page import="java.util.*" import="java.io.*" import="java.net.*"%>

<%
try{
		String url = "http://openapi.naver.com/search?key=2b997cf44caa9ccae89c33f86fb81899&query=";

		request.setCharacterEncoding("EUC-KR");
		String query = request.getParameter("query");
		query = URLEncoder.encode(query, "EUC-KR");
		url += query;
		
		String line = "";
		InputStream is = new URL(url).openStream();
		BufferedReader br = new BufferedReader(new InputStreamReader(is,
				"UTF-8"));
		StringBuilder sb = new StringBuilder();
		while ((line = br.readLine()) != null) {
			sb.append(line + "\n");
		}
		String data = sb.toString();

		ArrayList<String> array = new ArrayList<String>();

		String[] tags = new String[] { "title", "link" };
		int start = 0;
		boolean img = false;
		for (int i = 0; i < data.length(); i++) {
			if (data.charAt(i) == '<') {
				for (int j = 0; j < tags.length; j++) {
					if (data.substring(i + 1, i + 1 + tags[j].length())
							.equalsIgnoreCase(tags[j])) {
						start = i;
						img = true;
					} else if (img
							&& ((String) data.subSequence(i + 2, i + 2
									+ tags[j].length()))
									.equalsIgnoreCase(tags[j])) {
						array.add(data.substring(
								start + 2 + tags[j].length(), i));
						img = false;
					}
				}
			}
		}


	%>
	<table border="1">
		<%
			for (int i = 3; i < array.size(); i += 2) {
		%>
		<tr>
			<td><%=array.get(i - 1)%></td>

			<td><img src="<%=array.get(i)%>" width="200"></td>

		</tr>
		</table><%}
		}catch(Exception e){
			e.printStackTrace();
		}
%>